Completed
Push — master ( 866116...4a55ed )
by Ajeh
36s
created

directives.js ➔ ... ➔ Directives.init   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 4
rs 10
nop 1
1
// Directives
2
3
import {noop} from './utilities'
4
5
export default function (Vue) {
6
    let Directives = function () {}
7
8
    Directives.prototype.init = function (Vue) {
9
        this.Vue = Vue
10
        this.registerConfirm()
11
    }
12
13
    Directives.prototype.registerConfirm = function () {
14
        const _this = this
15
16
        const clickHandler = function (event, el, binding) {
17
            event.preventDefault()
18
            event.stopImmediatePropagation()
19
20
            let confirmMessage = (function () {
21
                if (binding.value && binding.value.message) {
22
                    return binding.value.message
23
                }
24
                return typeof binding.value === 'string' ? binding.value : null
25
            })()
26
27
            let thenCallback = (function () {
28
                if (binding.value && binding.value.ok) {
29
                    return binding.value.ok
30
                } else {
31
                    return () => {
32
                        el.removeEventListener('click', el.VuejsDialog.confirmHandler, true)
33
34
                        _this.Vue.nextTick(() => {
35
                            (function (node) {
36
                                if (document.createEvent) {
37
                                    let evt = document.createEvent('MouseEvents');
38
                                    evt.initEvent('click', true, false);
39
                                    node.dispatchEvent(evt);
40
                                } else if (document.createEventObject) {
41
                                    node.fireEvent('onclick');
42
                                } else if (typeof node.onclick === 'function') {
43
                                    node.onclick();
44
                                }
45
                            })(el)
46
47
                            el.addEventListener('click', el.VuejsDialog.confirmHandler, true)
48
                        })
49
                    }
50
                }
51
            })()
52
53
            let catchCallback = (function () {
54
                if (binding.value && binding.value.cancel) {
55
                    return binding.value.cancel
56
                }
57
                return noop
58
            })()
59
60
            _this.Vue.dialog.confirm(confirmMessage).then(thenCallback).catch(catchCallback)
61
        }
62
63
        this.Vue.directive('confirm', {
64
            bind (el, binding) {
65
                if (el.VuejsDialog === undefined) {
66
                    el.VuejsDialog = {}
67
                }
68
69
                el.VuejsDialog.confirmHandler = function clickEventHandler(event) {
70
                    clickHandler(event, el, binding)
71
                }
72
73
                el.addEventListener('click', el.VuejsDialog.confirmHandler, true)
74
            },
75
            unbind (el) {
76
                el.removeEventListener('click', el.VuejsDialog.confirmHandler, true)
77
            }
78
        })
79
80
81
    }
82
83
    let D = new Directives()
84
    D.init(Vue)
85
}